//
//  YEventListTableViewController.m
//  shijianpinggu
//
//  Created by apple on 2017/1/4.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YEventListTableViewController.h"
#import "YEventListTableViewCell.h"
#import "YEventInforMTTableViewController.h"
#import "eventListModel.h"
#import "YEventDTableViewController.h"
#import "YDangerDtTableViewController.h"

@interface YEventListTableViewController ()
@property(nonatomic,strong)eventListModel *elmodel;
@property(nonatomic,strong)NSMutableArray *dataAr;
@end

@implementation YEventListTableViewController
-(NSMutableArray *)dataAr{
    if(_dataAr==nil){
        self.dataAr = [[NSMutableArray alloc] init];
    }
    return _dataAr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.eventName;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    //去掉返回按钮上的文字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YEventListTableViewCell" bundle:nil] forCellReuseIdentifier:@"eventListCell"];
    
    [self loadDataFormUrl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%lu",(unsigned long)self.dataAr.count);
    return self.dataAr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YEventListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eventListCell" forIndexPath:indexPath];
    
    eventListModel *model = self.dataAr[indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"z"]];
    cell.titleName.text = model.type;
    cell.webName.text = model.title;
    cell.dayTime.text = model.created_at;
    cell.dangerJB.text = model.level;
    
    if ([cell.dangerJB.text isEqualToString:@"安全"]) {
        [cell.dangerJB setTextColor:[UIColor colorWithRed:0/255.0 green:110/255.0 blue:171/255.0 alpha:1]];
    }else if([cell.dangerJB.text isEqualToString:@"低危"]){
        [cell.dangerJB setTextColor:[UIColor colorWithRed:252/255.0 green:151/255.0 blue:0/255.0 alpha:1]];
    }else if([cell.dangerJB.text isEqualToString:@"中危"]){
        [cell.dangerJB setTextColor:[UIColor colorWithRed:255/255.0 green:17/255.0 blue:0/255.0 alpha:1]];
    }else if([cell.dangerJB.text isEqualToString:@"高危"]){
        [cell.dangerJB setTextColor:[UIColor colorWithRed:205/255.0 green:0/255.0 blue:0/255.0 alpha:1]];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    eventListModel *model = self.dataAr[indexPath.row];
    if ([self.typeTwo isEqualToString:@"fengxian"]) {
        YDangerDtTableViewController *dangerDVC = [YDangerDtTableViewController new];
        dangerDVC.type_v = model.type_v;
        dangerDVC.type_id = model.type_id;
        [self showViewController:dangerDVC sender:nil];
            }else{
        
        YEventInforMTTableViewController *eventDVC =[YEventInforMTTableViewController new];
        eventDVC.type_v = model.type_v;
        eventDVC.type_id = model.type_id;
                
        [self showViewController:eventDVC sender:nil];

    }
    
}
//请求数据
-(void)loadDataFormUrl{
    
    
    NSDictionary *parameter = @{@"type":self.type,@"site_id":self.site_id};
    
    //    NSLog(@"%@",dict);
    
    // 初始化Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 不加上这句话，会报“Request failed: unacceptable content-type: text/plain”错误，因为我们要获取text/plain类型数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // post请求
    [manager POST:@"http://wangan.ldynet.cn/index.php/IndexApp/Api_list" parameters:parameter constructingBodyWithBlock:^(id  _Nonnull formData) {
        // 拼接data到请求体，这个block的参数是遵守AFMultipartFormData协议的。
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 请求成功，解析数据
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSLog(@"aaaaaaaa%@", dic);
        NSArray *evAr = dic[@"list"];
        
        ;
        
        for (NSDictionary *dic in evAr) {
            eventListModel *emodel = [[eventListModel alloc] init];
            [emodel setValuesForKeysWithDictionary:dic];
            [self.dataAr insertObject:emodel atIndex:0];
        }
        
       
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 请求失败
        // [self alertWithString:@"请稍后再试"];
    }];
    
    
    //[self loginUrl];
}
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
